package com.ruoyi.smart.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.smart.config.CouchDBConfig;
import com.ruoyi.smart.domain.SmartProcessData;
import com.ruoyi.smart.service.ICouchdbSmartProcessData;
import org.ektorp.CouchDbConnector;

import org.ektorp.ViewQuery;
import org.ektorp.ViewResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Component
public class CouchDBService {
    private static final Logger log = LoggerFactory.getLogger(CouchDBService.class);
    @Autowired
    private CouchDBConfig couchDBConfig;
    private CouchDbConnector connector;

    private String db_name="";

    /**
     * 连接数据库
     * @throws Exception
     */
    @Bean(name = "CouchDbConnectorDB")
    public void getCouchDbConnector() throws Exception {
        db_name="smart_db";
        connector=couchDBConfig.couchDbConnector(db_name);
    }

    /**
     * 新增
     * @param data
     */
    public String insert(Object data) {
        CouchDBData couchDBData=new CouchDBData();
        if(data!=null){
            couchDBData.setData(data);
        }else {
            return null;
        }
        Date date = new Date();//获取当前的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String str = df.format(date);//获取String类型的时间
        couchDBData.setId(str+UUID.randomUUID().toString());
        connector.create(couchDBData);
        return couchDBData.getId();
    }


    /**
     * 修改
     * @param id
     * @param data
     */
    public String update(String id,Object data) {
        if(id==null) return insert(data);
        try {
            CouchDBData couchDBData=new CouchDBData();
            couchDBData=connector.get(CouchDBData.class,id);
            couchDBData.setData(data);
            connector.update(couchDBData);
            return couchDBData.getId();
        }catch (Exception e){
            log.error("couchDB数据库"+db_name+"找不到主键为:"+id+"的数据,无法更新");
            return id;
        }


    }

    /**
     * 删除
     * @param id
     */
    public String delete(String id) {
        try {
            CouchDBData couchDBData=new CouchDBData();
            couchDBData=connector.get(CouchDBData.class,id);
            connector.delete(couchDBData);
            return couchDBData.getId();
        }catch (Exception e){
            log.error("couchDB数据库"+db_name+"找不到主键为:"+id+"的数据,无法删除");
            return null;
        }
    }


    /**
     * 按id获取某个数据
     * @param id
     * @return
     */
    public Object get(String id){
        if(id==null) return id;
        try {
            CouchDBData couchDBData=connector.get(CouchDBData.class,id);
            return couchDBData.getData();
        }catch (Exception e){
            log.error("couchDB数据库"+db_name+"找不到主键为:"+id+"的数据");
            return id;
        }
    }


    /**
     * 按id获取某个数据
     * @param id
     * @return
     */
    public String getString(String id){
        if(id==null) return id;
        try {
            CouchDBData couchDBData=connector.get(CouchDBData.class,id);
            return String.valueOf(couchDBData.getData());
        }catch (Exception e){
            log.error("couchDB数据库"+db_name+"找不到主键为:"+id+"的数据");
            return id;
        }
    }

    /**
     * 获取视图信息
     * //这个还需实现
     */
    private void getViews() {
        ViewQuery query = new ViewQuery().viewName("Id").designDocId("_design/mrView").limit(1)/**.includeDocs(true)*/;
        ViewResult result = connector.queryView(query);
        System.out.println("查出来了"+JSON.toJSONString(result.getRows()));

    }



}
